package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.Landlord.Adapter.FacilityAdapter_L;
import com.weiyi.mashangzhu.Landlord.Adapter.FacilityAdapter_L_Edit;
import com.weiyi.mashangzhu.Landlord.Bean.FacilityBean_L;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Bean.FlagBean;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.baidu.mapapi.BMapManager.getContext;

public class FacilityActivity_L extends BaseActivity {

    @BindView(R.id.lv)
    ListView lv;
    List<FacilityBean_L> list = new ArrayList<>();
    List<Integer> list_select = new ArrayList<>();
    String info, id;
    String[] strings = null;
    FacilityAdapter_L facilityadapter = null;
    FacilityAdapter_L_Edit facilityadapter2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facility__l);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        info = intent.getStringExtra("info");
        id = intent.getStringExtra("id");

        list.add(new FacilityBean_L(R.mipmap.wifi, "无线宽带", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.tv, "电视", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.hot24, "全天热水", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.shower, "淋浴", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.washer, "洗衣机", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.lift, "电梯", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.menjin, "门禁", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.air, "空调", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.heating, "暖气", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.slipper, "拖鞋", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.cooking, "厨房", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.wash, "洗漱用品", R.mipmap.duo1));
        list.add(new FacilityBean_L(R.mipmap.smoking, "允许吸烟", R.mipmap.duo1));

        if (info.equals("post")) {
            facilityadapter = new FacilityAdapter_L(list, this);
            lv.setAdapter(facilityadapter);
        } else {
            LoadingDialog.show(this);
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(this, "token", "") + "")
                    .addParams("id", id)
                    .url(API.BaseApi_L + API.GetMatches)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            LoadingDialog.dismiss(FacilityActivity_L.this);
                            T.showShort(FacilityActivity_L.this, "数据获取失败，请稍后再试");
                        }

                        @Override
                        public void onResponse(String response) {

                            Log.e("ffffffff","re="+response);
                            LoadingDialog.dismiss(FacilityActivity_L.this);
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                if (jsonObject.getInt("status") == 0) {
                                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                                    JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                                    for (int i = 0; i < list.size(); i++) {
                                        MyApp.flagBeanList_edit.add(new FlagBean(false));
                                    }
                                    if (jsonObject1.getString("hMatches")==null||jsonObject1.getString("hMatches").equals("null")){

                                    }else {
                                        strings = jsonObject1.getString("hMatches").split(",");

                                        for (int i = 0; i < strings.length; i++) {
                                            list.get(Integer.parseInt(strings[i])-1).setImg(R.mipmap.duo);
                                            MyApp.flagBeanList_edit.get(Integer.parseInt(strings[i])-1).setFlag(true);
                                        }

                                    }
                                    facilityadapter2 = new FacilityAdapter_L_Edit(list, FacilityActivity_L.this);
                                    lv.setAdapter(facilityadapter2);

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        }


    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
//这里重写返回键
            MyApp.facility="";
            MyApp.facility = "";
            MyApp.flagBeanList_edit.clear();
            MyApp.flagBeanList.clear();
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.back, R.id.baocun})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                MyApp.facility="";
                MyApp.facility = "";
                MyApp.flagBeanList_edit.clear();
                MyApp.flagBeanList.clear();
                finish();
                break;
            case R.id.baocun:
                if (info.equals("post")) {
                    for (int i = 0; i < MyApp.flagBeanList.size(); i++) {
                        if (MyApp.flagBeanList.get(i).isFlag() == true) {
                            list_select.add(i+1);
                        }
                    }
                    Intent intent = new Intent();
                    if (list_select.size() != 0 || MyApp.flagBeanList.contains(true)) {
                        intent.putExtra("info", "完成");
                        MyApp.facility = "";
                        for (int i = 0; i < list_select.size() - 1; i++) {
                            MyApp.facility += list_select.get(i) + ",";
                        }
                        MyApp.facility += list_select.get(list_select.size() - 1);
                        Log.e("vvvvv", "size=" +  MyApp.facility);
                    } else {
                        intent.putExtra("info", "未完成");
                        Log.e("hhhhhh", "未完成");
                    }

                    setResult(0x008, intent);
                    finish();
                } else {

                    if (MyApp.flagBeanList_edit.size() == 0) {
                        finish();
                    } else {
                        for (int i = 0; i < MyApp.flagBeanList_edit.size(); i++) {
                            if (MyApp.flagBeanList_edit.get(i).isFlag() == true) {
                                list_select.add(i+1);
                            }
                        }

                        if (list_select.size() != 0 || MyApp.flagBeanList.contains(true)) {
                            String facility = "";
                            for (int i = 0; i < list_select.size() - 1; i++) {
                               facility += list_select.get(i) + ",";
                            }
                          facility += list_select.get(list_select.size() - 1);
                            Log.e("ffffffff", "size=" + facility);
                            LoadingDialog.show(FacilityActivity_L.this);
                            OkHttpUtils.get()
                                    .addParams("token",SPUtils.get(FacilityActivity_L.this,"token","")+"")
                                    .addParams("id",id)
                                    .addParams("hMatches",facility)
                                    .url(API.BaseApi_L+API.UpdateMatches)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            LoadingDialog.dismiss(FacilityActivity_L.this);
                                            T.showShort(FacilityActivity_L.this,"数据获取失败，请稍后再试");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            LoadingDialog.dismiss(FacilityActivity_L.this);
                                            try {
                                                JSONObject js=new JSONObject(response);
                                                if (js.getInt("status")==0){
                                                    MyApp.facility = "";
                                                    MyApp.flagBeanList_edit.clear();
                                                    MyApp.flagBeanList.clear();
                                                    T.showShort(FacilityActivity_L.this,js.getString("msg") + " 后台审核中...");
                                                    finish();
                                                } else {
                                                    T.showShort(FacilityActivity_L.this,js.getString("msg"));
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            Log.e("fffffffffffff","re"+response);

                                        }
                                    });


                        } else {
                            LoadingDialog.show(FacilityActivity_L.this);
                            OkHttpUtils.get()
                                    .addParams("token",SPUtils.get(FacilityActivity_L.this,"token","")+"")
                                    .addParams("id",id)
                                    .addParams("hMatches","")
                                    .url(API.BaseApi_L+API.UpdateMatches)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            LoadingDialog.dismiss(FacilityActivity_L.this);
                                            T.showShort(FacilityActivity_L.this,"数据获取失败，请稍后再试");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            LoadingDialog.dismiss(FacilityActivity_L.this);
                                            try {
                                                JSONObject js=new JSONObject(response);
                                                if (js.getInt("status")==0){
                                                    MyApp.facility = "";
                                                    MyApp.flagBeanList_edit.clear();
                                                    MyApp.flagBeanList.clear();
                                                    T.showShort(FacilityActivity_L.this,js.getString("msg") + " 后台审核中...");
                                                    finish();
                                                } else {
                                                    T.showShort(FacilityActivity_L.this,js.getString("msg"));
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            Log.e("fffffffffffff","re"+response);

                                        }
                                    });
                        }
                    }
                }

                break;
        }
    }
}
